package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/21 21:28
 * description:
 */
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsCategoryService categoryService;

    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit",required = false,defaultValue = "10") Integer limit,
            String sort,String order, String goodsSn,String name
    ){
        //获取当前登录的用户对象
        Subject subject = SecurityUtils.getSubject();
        //获取admin对象
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds=new ArrayList<>();
        //判断
        if(subject.hasRole("品牌制造商")){
            //如果是品牌制造商，查询该管理员
            brandIds=brandService.queryByAdminId(admin.getId());
        }
        //否则查询所有商品数据
        IPage<DtsGoods> ipage =goodsService.queryPage(page,limit,sort,order,goodsSn,name,brandIds);

        Map<String ,Object> data=new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);


    }


    @RequiresPermissions("admin:goods:read")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "详情")
    @GetMapping("/detail")
    public Object detail(Integer id){
        if(id==null){
            return AdminResponseUtil.badArgument();
        }
        //查询商品的详情信息
        GoodsAllinone data =goodsService.queryDetailById(id);
        return ResponseUtil.ok(data);

    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        //查询分类列表
        List<CatVo> catVos=categoryService.findCategoryList();

        //查询品牌数据
        List<Map<String,Object>> brands=brandService.findBrand();
        Map<String,Object> data=new HashMap<>();
        data.put("categoryList",catVos);
        data.put("brandList",brands);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        try {
            goodsService.update(goodsAllinone);
        } catch (Exception e) {
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:goods:detele")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        return goodsService.delete(goods);
    }

    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "新增")
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        try {
            goodsService.create(goodsAllinone);
        } catch (Exception e) {
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }
}
